package com.hellobike.haxing.modules.purchase.common;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by zhanglei on 18/6/6.
 */
public class TimerUtil {


    public static final String DATE_PATTERN = "yyyy-MM-dd";


    /**
     * 判断是否是工作日的时间段
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static boolean isWorkDay(Date time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println(imptimeBegin);
        cal.add(Calendar.DATE, 4);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println(imptimeEnd);
        //判断当前日期是否在周一至周五内
        long start = sdf.parse(imptimeBegin).getTime();
        long end = sdf.parse(imptimeEnd).getTime();
        long current = time.getTime();
        if (start < current && current < end) {
            //获取当前时间的9.00到16.00 判断是否在中间
            int hours = time.getHours();
            if (hours > 9 && hours < 16) {
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    /*获得当前时间*/
    public static String getCurrentTime(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /*时间比大小*/
    public static int timeCompare(String t1,String t2){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try {
            c1.setTime(formatter.parse(t1));
            c2.setTime(formatter.parse(t2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result=c1.compareTo(c2);
        return result;
    }

    public static String getLeftDate(int t) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -t);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getcurrent23Date() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(start);
    }

    public static Integer getWeekOfDate(Date date) {
//        String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
//        return weekDaysName[intWeek];
    }

    public static Long getDayByDay() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = df.parse("2020-03-17");
            Date date2 = df.parse("2020-03-16");
            return (date1.getTime() - date2.getTime()) / (7 * 24 * 60 * 60 * 1000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0l;
    }

    public static String getLast23Date() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, -1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(start);
    }

    public static Date getcurrent23DateTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }


    public static Date getLast23DateTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, -1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String getEnd23Date(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(start);
    }


    public static String getStart23Date(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, -1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(start);
    }

    public static String getSubDay(String endTime){
        long second = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //将时间转换为毫秒
            long nowSecond = new Date().getTime();
            long endTimeSecond = simpleDateFormat.parse(endTime).getTime();
            //  时间相减
            second = (endTimeSecond - nowSecond)/(1000*3600*24);
        }catch (Exception e){
            e.printStackTrace();
        }
        return String.valueOf(second);
    }

    public static String getLast30HoursDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -30);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    public static String getAfter24HoursDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    public static String getTodayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static long getTomorrowDatelong(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime().getTime();
    }

    public static long get6HoursDatelong(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 6);
        return calendar.getTime().getTime();
    }

    public static String getTomorrowDateString(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    public static String getSixHourDateString(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 6);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }


    public static String getMonEndDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(cale.getTime());
    }

    public static String getMonStartDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));
        return format.format(cale.getTime());
    }

    public static Date getTodayDateForD() {
        long current = System.currentTimeMillis();// 当前天时间毫秒数
        long zero = current / (1000 * 3600 * 48) * (1000 * 3600 * 48) - TimeZone.getDefault().getRawOffset();// 当前零点零分零秒的毫秒数
        return new Date(zero + 2 * 60 * 60 * 1000);
    }

    public static Date getLastDayDateForD() {
        long current = System.currentTimeMillis();// 当前天时间毫秒数
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();// 当前零点零分零秒的毫秒数
        return new Date(zero - 24 * 60 * 60 * 1000);
    }

    public static long getLeftSec(Date time) {
        long tomorrow = getTomorrowDatelong(time);
        long current = System.currentTimeMillis();
        long leftSec = tomorrow - current;
        return leftSec/1000;
    }

    public static long get6HoursLeftSec(Date time) {
        long tomorrow = get6HoursDatelong(time);
        long current = System.currentTimeMillis();
        long leftSec = tomorrow - current;
        return leftSec/1000;
    }

    public static String getNowDate() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }

    public static String addOneDay(String day) {
        try {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDay = f.parse(day);
            return new Timestamp(nowDay.getTime() + 24 * 60 * 60 * 1000).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }

    public static String subOneDay(String day) {
        try {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDay = f.parse(day);
            return new Timestamp(nowDay.getTime() - 24 * 60 * 60 * 1000).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }

    public static String dateConvert(Date now) {
        DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        return f.format(now);
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date fDate=sdf.parse("2020-03-03 10:05:06");
//        long days = (new Date().getTime() - fDate.getTime())/(1000*3600*24);
//        System.out.println(days);
//        getLeftSec(new Date());
//        System.out.println(getTomorrowDateString(new Date()));
//        System.out.println(new Date(System.currentTimeMillis()));
//        System.out.println(fDate);
//        System.out.println(getWeekOfDate(fDate));
        System.out.println(getDayByDay());

    }

}
